package com.by.controller;

import cn.hutool.core.bean.BeanUtil;
import com.by.bean.Freight;
import com.by.common.bean.PageBean;
import com.by.common.bean.ResultBean;
import com.by.common.controller.BaseController;
import com.by.common.group.V;
import com.by.common.utils.Assert;
import com.by.query.FreightQuery;
import com.by.service.FreightService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/api")
@Api(tags = "货代管理")
public class FreightController extends BaseController {
    @Autowired
    FreightService freightService;
    @ApiOperation("查询货代信息")
    @GetMapping("/freight")
    public ResultBean get(@RequestParam(required = true) Serializable id){
        Freight freight = freightService.get(id);
        return success("客户信息",freight);
    }
    @ApiOperation("保存货代信息")
    @PostMapping("/freight")
     public ResultBean save(@Validated @RequestBody Freight freight ,BindingResult result){
        V.validate(result);
        int rs = freightService.save(freight);
        return saveResult(rs);
    }
    @ApiOperation("删除货代信息")
    @DeleteMapping("/freight")
    public ResultBean delete(@RequestParam(required = true) Serializable id){
        int rs = freightService.delete(id);
        Assert.isZero(rs,"删除失败");
        return deleteResult(rs);
    }
    @ApiOperation("修改货代信息")
    @PutMapping("/freight")
    public ResultBean update(@RequestBody Freight freight){
        int rs = freightService.update(freight);
        Assert.isZero(rs,"修改失败");
        return updateResult(rs);
    }
    @ApiOperation("查询货代列表")
    @PostMapping("freight/list")
    public ResultBean<PageBean<List<Freight>>> query(@RequestBody FreightQuery query){
        PageBean bean = freightService.queryByMap(BeanUtil.beanToMap(query));
        return success("货代列表",bean);
    }
}
